package com.yanqu.road.dao.impl.nenmonopolymarket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.NewMonopolyMarketServerAbilityData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.NewMonopolyUserAbility;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NewMonopolyMarketServerAbilityDataDaoImpl extends TempDataObjectDao<NewMonopolyMarketServerAbilityData> {


    public static String getNewMonopolyMarketServerAbilityDataSql = "select * from `t_c_new_monopoly_market_server_data` where `weekId`=? and `systemGroupId`=?";
    public Map<Long, NewMonopolyMarketServerAbilityData> getNewMonopolyMarketServerAbilityData(int weekId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, NewMonopolyMarketServerAbilityData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewMonopolyMarketServerAbilityDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setLong(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewMonopolyMarketServerAbilityData data = parse(rs);
                map.put(data.getServerId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateNewMonopolyMarketServerAbilityDataSql = "update `t_c_new_monopoly_market_server_data` set activeNum=?,resetTime=?,userAbilityList=? where systemGroupId=? and weekId=? and serverId=?";
    @Override
    protected boolean updateData(NewMonopolyMarketServerAbilityData newMonopolyMarketServerAbilityData) {
        newMonopolyMarketServerAbilityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketServerAbilityData.getActiveNum()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketServerAbilityData.getResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketServerAbilityData.getUserAbilityList())));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketServerAbilityData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketServerAbilityData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketServerAbilityData.getServerId()));
        if (getDb().executeNoneQuery(updateNewMonopolyMarketServerAbilityDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewMonopolyMarketServerAbilityDataSql = "insert into `t_c_new_monopoly_market_server_data`(systemGroupId,weekId,serverId,activeNum,resetTime,userAbilityList) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(NewMonopolyMarketServerAbilityData newMonopolyMarketServerAbilityData) {
        newMonopolyMarketServerAbilityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketServerAbilityData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketServerAbilityData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketServerAbilityData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketServerAbilityData.getActiveNum()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketServerAbilityData.getResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketServerAbilityData.getUserAbilityList())));
        if (getDb().executeNoneQuery(insertNewMonopolyMarketServerAbilityDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewMonopolyMarketServerAbilityData parseData(ResultSet rs) throws SQLException {
        NewMonopolyMarketServerAbilityData newMonopolyMarketServerAbilityData = new NewMonopolyMarketServerAbilityData();
        newMonopolyMarketServerAbilityData.setGroupId(rs.getLong("systemGroupId"));
        newMonopolyMarketServerAbilityData.setWeekId(rs.getInt("weekId"));
        newMonopolyMarketServerAbilityData.setServerId(rs.getLong("serverId"));
        newMonopolyMarketServerAbilityData.setActiveNum(rs.getInt("activeNum"));
        newMonopolyMarketServerAbilityData.setResetTime(rs.getInt("resetTime"));
        newMonopolyMarketServerAbilityData.setUserAbilityList(JSON.parseObject(rs.getString("userAbilityList"), new TypeReference<List<NewMonopolyUserAbility>>() {}));

        return newMonopolyMarketServerAbilityData;
    }
}
